﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class application_step21 : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);
        InitializeUI();
        LoadPlacementTypes(langId);
        LoadVehicleTypes(langId);
        LoadPackingTypes(langId);
        LoadGoodsTypes(langId);
        LoadSpecialConditionTypes(langId);
        LoadAttributes();
        LoadWeightTypes(langId);
    }
    
    private void LoadWeightTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 20);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlWeight.Items.Clear();
                while (dr.Read())
                {
                    ddlWeight.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }
                dr.Close();
            }
        }
    }


    private void LoadAttributes()
    {
        //ddlTransportTypes.Attributes.Add("validate", "true");
        ddlPackingTypes.Attributes.Add("validate", "true");
        ddlGoods.Attributes.Add("validate", "true");
        tbWeightOfGoods.Attributes.Add("validate", "true");
        ddlPlacementTypes.Attributes.Add("validate", "true");
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    private void LoadPlacementTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 8);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPlacementTypes.Items.Clear();
                ddlPlacementTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlPlacementTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadVehicleTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,10000,'id','desc') WHERE isActive = 'true'", conn))
            {
                long type_id = 0;
                if (Request.QueryString["typeid"] != null)
                {
                    long.TryParse(Request.QueryString["typeid"].ToString(), out type_id);
                }
                
                switch (type_id)
                {
                    case 1:
                        lbl_type_of_transport.Text = DAO.TextBaseFactory.loadText("craft_type", this.LangId);
                        break;
                    case 3:
                        lbl_type_of_transport.Text = DAO.TextBaseFactory.loadText("vessel_type", this.LangId);
                        break;
                    case 14:
                    case 13:
                    default:
                        lbl_type_of_transport.Text = DAO.TextBaseFactory.loadText("vehicle_type", this.LangId);
                        break;

                }
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", type_id);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlTransportTypes.Items.Clear();
                ddlTransportTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlTransportTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlTransportTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadPackingTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 6);
               
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlPackingTypes.Items.Clear();
                ddlPackingTypes.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlPackingTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlPackingTypes.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    /*private void LoadCountry()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetAllPlaces(0, 1000000, 'id', 'asc', 2) WHERE isActive='true' Order By name asc", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddlCountryLoading.Items.Clear();
                ddlCountryUnloading.Items.Clear();
                ddlCountryLoading.Items.Add(new ListItem("", "0"));
                ddlCountryUnloading.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddlCountryLoading.Items.Add(new ListItem(dr["short"].ToString() + " - " + dr["name"].ToString(), dr["id"].ToString()));
                    ddlCountryUnloading.Items.Add(new ListItem(dr["short"].ToString() + " - " + dr["name"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }*/

    private void LoadGoodsTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 10);
                
                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlGoods.Items.Clear();
                ddlGoods.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlGoods.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlGoods.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadSpecialConditionTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 5);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddlSprecialCondition.Items.Clear();
                ddlSprecialCondition.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddlSprecialCondition.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddlSprecialCondition.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }
}
